package com.zking.dao;

import com.zking.dao.UserDao;
import com.zking.entity.News;
import com.zking.entity.User;
import com.zking.util.DBUtil;

import java.util.List;

/**
 * UserDao 类用于操作用户表 easybuy_user 的数据访问对象。
 */
public class UserDao {

    /**
     * 根据用户名和密码查询用户信息
     * @param eu_user_id 用户名
     * @param eu_password 密码
     * @return 查询到的用户对象，若未查询到则返回 null
     */
    public void registerUser(User user) {
        String sql =
                "INSERT INTO easybuy_user " +
                        "(eu_user_id, eu_user_name, eu_password, eu_sex, " +
                        "eu_birthday, eu_identity_code, eu_email, eu_mobile, " +
                        "eu_address, eu_status, is_deleted) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        DBUtil.update(sql, user.getEu_user_id(), user.getEu_user_name(), user.getEu_password(),
                user.getEu_sex(), user.getEu_birthday(), user.getEu_identity_code(), user.getEu_email(),
                user.getEu_mobile(), user.getEu_address(), user.getEu_status(), 0);
    }

    public static User getUser(String eu_user_id, String eu_password) {
        System.out.println("请求用户表");
        System.out.println(eu_user_id + "\t" + eu_password);
        String sql = "select * from easybuy_user where eu_user_id=? and eu_password=? and is_deleted=0";
        List<User> list = DBUtil.query(sql, User.class, eu_user_id, eu_password);
        if (list.size() == 1) {
            return list.get(0);
        }
        return null;


    }

    /**
     * 分页查询用户列表
     * @param pageIndex 当前页码
     * @param pageSize 每页显示的记录数
     * @return 当前页的用户列表
     */
    public static List<User> getUsersByPage(int pageIndex, int pageSize) {
        String sql = "select * from easybuy_user where is_deleted=0 limit ?,?";
        return DBUtil.query(sql, User.class, (pageIndex - 1) * pageSize, pageSize);
    }

    /**
     * 计算用户表的最大页数
     * @param pageSize 每页显示的记录数
     * @return 用户表的最大页数
     */
    public static int getMaxPage(int pageSize) {
        List<User> list = getAllUsers();
        int total = list.size();
        int maxPage = total / pageSize;
        if (total % pageSize != 0) {
            maxPage++;
        }
        return maxPage;
    }

    /**
     * 查询所有用户
     * @return 用户列表
     */
    public static List<User> getAllUsers() {
        String sql = "select * from easybuy_user where is_deleted=0";
        return DBUtil.query(sql, User.class);
    }

    /**
     * 根据用户ID查询用户信息
     * @param id 用户ID
     * @return 查询到的用户对象，若未查询到则返回 null
     */
    public static User getUserById(String id) {
        String sql = "select * from easybuy_user where eu_user_id=? and is_deleted=0";
        List<User> list = DBUtil.query(sql, User.class, id);
        if (list.size() == 1) {
            return list.get(0);
        }
        return null;
    }

    public static boolean deleteUser(String id) {
        String sql = "update easybuy_user set is_deleted=1 where eu_user_id=?";
        return DBUtil.update(sql, id);
    }

    /**
     * 更新用户信息
     * @param user 要更新的用户对象
     * @return 更新操作是否成功
     */
    public static boolean updateUser(User user) {
        String sql = "update easybuy_user set eu_user_name=?, eu_password=?, " +
                "eu_sex=?, eu_birthday=?, eu_identity_code=?, eu_email=?, eu_mobile=?, " +
                "eu_address=? where eu_user_id=? and is_deleted=0";
        return DBUtil.update(sql, user.getEu_user_name(), user.getEu_password(), user.getEu_sex(), user.getEu_birthday(),
                user.getEu_identity_code(), user.getEu_email(), user.getEu_mobile(), user.getEu_address(),
                user.getEu_user_id());
    }

    public static boolean addUser(User user) {
        String sql = "insert into easybuy_user values(?,?,?,?,?,?,?,?,?,1,0)";
        return DBUtil.update(sql, user.getEu_user_id(),user.getEu_user_name(), user.getEu_password(), user.getEu_sex(), user.getEu_birthday(),
                user.getEu_identity_code(), user.getEu_email(), user.getEu_mobile(), user.getEu_address());
    }

    public static boolean checkUser(String id){
        String sql = "select * from easybuy_user where eu_user_id=?";
        List<User> user = DBUtil.query(sql, User.class, id);
        return user.isEmpty();
    }
}
